using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace StudentManagementApplication
{
    public partial class FrmFee : Form
    {
        List<Entities.Fee> feeList = new List<Entities.Fee>();
        List<Entities.Class> classList = new List<Entities.Class>();
        List<Entities.FeeType> feeTypeList = new List<Entities.FeeType>();
        Business.FeeService feeService = new Business.FeeService();
        Business.ClassService classService = new Business.ClassService();
        Business.FeeTypeService feeTypeService = new Business.FeeTypeService();
        Entities.Fee fee = new Entities.Fee();
        int iIndex = 0;

        public FrmFee()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            InitializeStudentClass();
            InitializeStatus();
        }

        private void InitializeStudentClass()
        {
            classList = classService.GetAllClasses();
            foreach (Entities.Class _class in classList)
            {
                cmbClass.Items.Add(_class.ClassID + "--" + _class.ClassName);
            }
        }

        private void InitializeFeeType()
        {
            cmbFeeType.Items.Clear();
            feeTypeList = feeTypeService.GetAllFeeTypes();
            foreach(Entities.FeeType feeType in feeTypeList)
            {
                cmbFeeType.Items.Add(feeType.TypeID + "--" + feeType.TypeName);
            }
        }

        private void InitializeStatus()
        {
            cmbStatus.Items.Add("Yes");
            cmbStatus.Items.Add("No");
        }

        private void ShowDataGirdView()
        {
            
            if (cmbStatusOfFee.Text == string.Empty && cmbClass.Text != string.Empty)
            {
                using (DataTable dataTable = feeService.GetFeeByClassID(cmbClass.Text.Substring(0, 6)))
                {
                    ShowData(dataTable); ;
                }
            }
            else
                if (cmbStatusOfFee.Text != string.Empty && cmbClass.Text == string.Empty)
                {
                    using (DataTable dataTable = feeService.GetFeeByStatus(GetStatusOfFee(cmbStatusOfFee.Text)))
                    {
                        ShowData(dataTable);
                    }
                }
                else
                {
                    using (DataTable dataTable = feeService.GetFeeByStatusAndClassID(GetStatusOfFee(cmbStatusOfFee.Text), cmbClass.Text.Substring(0, 6)))
                    {
                        ShowData(dataTable);
                    }
                }
            dgvStudentFee[0, iIndex].Selected = true;
        }

        private void ShowData(DataTable dataTable)
        {
            if (dgvStudentFee.Rows.Count > 1)
            {
                int iCount = dgvStudentFee.Rows.Count - 1;
                for (int i = 0; i < iCount; i++)
                {
                    dgvStudentFee.Rows.RemoveAt(0);
                }
            }
            foreach (DataRow row in dataTable.Rows)
            {
                string[] temp = { row[0].ToString(), row[1].ToString(), row[2].ToString(),int.Parse(row[3].ToString())>0?"Paid":"UnPaid" };
                dgvStudentFee.Rows.Add(temp);
            }
        }

        private void FindFeeByStudentID(string sStudentID)
        {
            using (DataTable dataTable = feeService.GetAllFees2())
            {
                if (dgvStudentFee.Rows.Count > 1)
                {
                    int iCount = dgvStudentFee.Rows.Count - 1;
                    for (int i = 0; i < iCount; i++)
                    {
                        dgvStudentFee.Rows.RemoveAt(0);
                    }
                }
                foreach (DataRow row in dataTable.Rows)
                {
                    if (row[0].ToString().ToLower().Contains(sStudentID))
                    {
                        string[] temp = { row[0].ToString(), row[1].ToString(), row[2].ToString(), int.Parse(row[3].ToString()) > 0 ? "Paid" : "UnPaid" };
                        dgvStudentFee.Rows.Add(temp);
                    }
                }
            }
            iIndex = 0;
            dgvStudentFee[0, iIndex].Selected = true;
            if (dgvStudentFee.Rows.Count > 1) FindStudentFee(dgvStudentFee.Rows[iIndex].Cells[0].Value.ToString());
        }

        private void FindStudentFee(string sStudentID)
        {
            fee = feeService.GetFeeByStudentID(sStudentID);
            txtStudentID.Text = fee.StudentID;
            cmbFeeType.Text = fee.FeeType;
            if (fee.FeePaid > 0)
                cmbStatus.SelectedIndex = 0;
            else
                cmbStatus.SelectedIndex = 1;
        }

        private int GetStatusOfFee(string sStatus)
        {
            int iResult = 0;
            switch (sStatus)
            {
                case "UnPaided": iResult = 0;
                    break;
                case "Paided": iResult = 1;
                    break;
                default:
                    break;
            }
            return iResult;
        }

        private bool Test()
        {
            bool bResult = true;
            string sInformation = string.Empty;
            if (cmbFeeType.Text == string.Empty)
                sInformation += " . Fee type 's empty" + "\n";
            if (cmbStatus.Text == string.Empty)
                sInformation += " . Status of payment 's empty" + "\n";
            if (sInformation != string.Empty)
            {
                MessageBox.Show(sInformation, "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                bResult = false;
            }
            return bResult;
        }

        private void button1_Click(object sender, EventArgs e)
        {
           
        }

        private void cmbClass_SelectedIndexChanged(object sender, EventArgs e)
        {
            ShowDataGirdView();
        }

        private void cmbStatusOfFee_SelectedIndexChanged(object sender, EventArgs e)
        {
            ShowDataGirdView();
            
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            FindFeeByStudentID(txtStudentID.Text.ToLower());
        }

        private void chkIsPaid_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void dgvStudentFee_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dgvStudentFee.CurrentRow.Index < dgvStudentFee.Rows.Count - 1)
            {
                iIndex = dgvStudentFee.CurrentRow.Index;
                string sStudentID = dgvStudentFee.CurrentRow.Cells[0].Value.ToString();
                FindStudentFee(sStudentID);
                InitializeFeeType();
            }
        }

        private void btExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (fee != null )
            {
                if (Test())
                {
                    fee.FeePaid = (cmbStatus.SelectedIndex == 0 ? 1 : 0);
                    fee.FeeType = (cmbFeeType.Text.Length > 3 ? cmbFeeType.Text.Substring(0, 3) : cmbFeeType.Text);
                    feeService.UpdateFee(fee);
                    ShowDataGirdView();
                }
            }
            else
                MessageBox.Show("You should choose the fee yoi want to save");
        }
    }
}